﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques;
using Tecnopez.Globals;
using BussinessLogicTecnoPez;
using System.Collections.Generic;

public partial class Modules_EnfermedadesControlEstanque_ABMSensores : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            SetMode(PageMode.ListMode);
        }

        InicializarGrilla();

        cmdEliminar.OnClientClick = "return confirm('Esta seguro de eliminar el registro?');";

        GenericMasterPage master = (GenericMasterPage)this.Master;
        master.Imagen = "../Styles/Icons/tools.png";
        master.Titulo = "Gestión de Sensores";
        cargarGrilla();
        _lblError.Text = "";
                
        this.tppTipoPuerto.ObjType = typeof(TipoPuerto);
        this.tppParametroControl.ObjType = typeof(ParametroControl);
    }

    private void InicializarGrilla()
    {
        DataControlField[] extraButtons = new DataControlField[1];
        ButtonField colB = new ButtonField();
        colB.ButtonType = ButtonType.Image;
        colB.ImageUrl = Functions.getHost(this.Page) + "Styles/Icons/chart.png";
        colB.HeaderText = "Ver resultados";
        colB.CommandName = "RESULTADOSSENSOR";
        colB.HeaderStyle.Width = 100;
        colB.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        extraButtons[0] = colB;
        grdSensores.setupGrid(typeof(Sensor), cargarGrilla, true, true, this.ViewState, extraButtons);
    }

    private void SetMode(PageMode mode)
    {
        ViewState["PageMode"] = mode;
        switch (mode)
        {
            case PageMode.ListMode:
                mvABM.ActiveViewIndex = 0;
                break;
            case PageMode.NewMode:
                mvABM.ActiveViewIndex = 1;
                txtCodigo.ReadOnly = true;
                txtDescripcion.ReadOnly = false;
                txtNumeroPuerto.ReadOnly = false;
                tppParametroControl.Enable = true;
                tppTipoPuerto.Enable = true;
                cmdAceptar.Visible = true;
                cmdCancelar.Enabled = true;
                cmdEditar.Enabled = false;
                cmdEliminar.Enabled = false;
                break;
            case PageMode.EditMode:
                mvABM.ActiveViewIndex = 1;
                txtCodigo.ReadOnly = true;
                txtDescripcion.ReadOnly = false;
                txtNumeroPuerto.ReadOnly = false;
                tppParametroControl.Enable = true;
                tppTipoPuerto.Enable=true;
                cmdAceptar.Visible = true;
                cmdCancelar.Enabled = true;
                cmdEditar.Enabled = false;
                cmdEliminar.Enabled = false;
                break;
            case PageMode.ViewMode:
                mvABM.ActiveViewIndex = 1;
                txtCodigo.ReadOnly = true;
                txtDescripcion.ReadOnly = true;
                txtNumeroPuerto.ReadOnly = true;
                tppParametroControl.Enable = false;
                tppTipoPuerto.Enable = false;
                cmdAceptar.Visible = false;
                cmdCancelar.Enabled = true;
                cmdEditar.Enabled = true;
                cmdEliminar.Enabled = true;
                break;
        }

    }

    private void cargarGrilla()
    {
        Sensor.getManager.ClearFilters();
        Sensor.getManager.ClearOrders();
        Sensor.getManager.AddFilter("Codigo", GenericManager<Sensor>.filterOperator.Like, "%" + txtBuscarCodigo.Text.Trim() + "%");
        Sensor.getManager.AddFilter("Descripcion", GenericManager<Sensor>.filterOperator.Like, "%" + txtBuscarDescripcion.Text.Trim() + "%");

        if (ViewState[grdSensores.PageDataViewOrderKey] == null)
        {
            Sensor.getManager.AddOrder("Codigo", GenericManager<Sensor>.Order.orderDirection.DESC);
        }
        else
        {
            Sensor.getManager.AddOrder(ViewState[grdSensores.PageDataViewOrderKey].ToString(), (GenericManager<Sensor>.Order.orderDirection)ViewState[grdSensores.PageDataViewOrdenDireccionKey]);
        }

        List<Sensor> lista = new List<Sensor>();
        lista = Sensor.getManager.GetFilter();
        grdSensores.DataSource = lista;
        grdSensores.DataBind();

    }

    protected void cmdAceptar_Click(object sender, EventArgs e)
    {
        try
        {
            Sensor objSensor = new Sensor();

            objSensor.NumeroPuerto = Convert.ToInt32(txtNumeroPuerto.Text.Trim());
            objSensor.Descripcion = txtDescripcion.Text.Trim();
            objSensor.ParametroControl = (ParametroControl) tppParametroControl.ItemSelected;
            objSensor.TipoPuerto = (TipoPuerto)tppTipoPuerto.ItemSelected;

                if (getMode() == PageMode.NewMode)
                {
                    if (Sensor.getManager.Insert(objSensor) != true)
                    {
                        throw Sensor.getManager.GeneratedException;
                    }
                    SetMode(PageMode.ListMode);
                    cargarGrilla();
                }
                else
                {
                    objSensor.Codigo = int.Parse(txtCodigo.Text.Trim());
                    if (Sensor.getManager.Update(objSensor) != true)
                    {
                        throw Sensor.getManager.GeneratedException;
                    }
                    SetMode(PageMode.ViewMode);
                }
        }
        catch (Exception exc)
        {
            _lblError.Text = "error: No se pudo realizar la operación";
            Functions.LogError(exc);
        }
    }
    protected void cmdCancelar_Click(object sender, EventArgs e)
    {
        SetMode(PageMode.ListMode);
    }
    protected void cmdEliminar_Click(object sender, EventArgs e)
    {
        try
        {
            Sensor objSensor = new Sensor();
            objSensor.Codigo = int.Parse(this.txtCodigo.Text.Trim());
            if (Sensor.getManager.Delete(objSensor) < 1)
            {
                throw Sensor.getManager.GeneratedException;
            }

            SetMode(PageMode.ListMode);
            cargarGrilla();
        }
        catch (Exception ex)
        {
            _lblError.Text = ex.Message;
        }      
    }
    protected void cmdEditar_Click(object sender, EventArgs e)
    {
        SetMode(PageMode.EditMode);
    }
    protected void cmdBuscar_Click(object sender, EventArgs e)
    {

    }
    protected void cmdNuevo_Click(object sender, EventArgs e)
    {
        SetMode(PageMode.NewMode);
        txtCodigo.Text = "";
        txtDescripcion.Text = "";
        txtNumeroPuerto.Text = "";
        tppParametroControl.ItemSelected = null;
        tppTipoPuerto.ItemSelected = null;
    }
    protected void grdSensores_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ver")
        {
            int IdSensor = int.Parse(grdSensores.DataKeys[System.Convert.ToInt32(e.CommandArgument)].Value.ToString());
            Sensor.getManager.ClearFilters();
            Sensor.getManager.AddFilter("Codigo", GenericManager<Sensor>.filterOperator.Equals, IdSensor);
            Sensor objSensor = Sensor.getManager.GetOne();
            txtCodigo.Text = objSensor.Codigo.ToString();
            txtDescripcion.Text = objSensor.Descripcion.Trim();
            txtNumeroPuerto.Text = objSensor.NumeroPuerto.ToString();
            tppTipoPuerto.ItemSelected = objSensor.TipoPuerto;
            tppParametroControl.ItemSelected = objSensor.ParametroControl;

            SetMode(PageMode.ViewMode);
        }
        if (e.CommandName == "RESULTADOSSENSOR")
        {
            int IdSensor = int.Parse(grdSensores.DataKeys[System.Convert.ToInt32(e.CommandArgument)].Value.ToString());
            Response.Redirect("ConsultarResultadosSensor.aspx?idsensor=" + IdSensor.ToString());
        }
    }
    private PageMode getMode()
    {
        if (ViewState["PageMode"] == null)
            return PageMode.ModeNotFound;
        return (PageMode)ViewState["PageMode"];
    }
}
